import { Inject, Body, Post } from '@midwayjs/core';
import { CoolController, BaseController } from '@cool-midway/core';
import { PackageInfoEntity } from '../../entity/info';
import { PackageInfoService } from '../../service/info';

/**
 * 套餐信息
 */
@CoolController({
  api: ['add', 'delete', 'update', 'info', 'list', 'page'],
  entity: PackageInfoEntity,
  service: PackageInfoService,
  pageQueryOp: {
    keyWordLikeFields: ['a.name', 'a.description'],
    fieldEq: ['a.price', 'a.status'],
    where: async ctx => {
      const { startTimeDate, endTimeDate } = ctx.request.body;
      const where = [];
      if (startTimeDate) {
        where.push(['a.startTimeDate >= :startTimeDate', { startTimeDate }]);
      }
      if (endTimeDate) {
        where.push(['a.endTimeDate <= :endTimeDate', { endTimeDate }]);
      }
      return where;
    },
  },
})
export class AdminPackageInfoController extends BaseController {
  @Inject()
  packageInfoService: PackageInfoService;

  @Post('/getPackageTotalMoney', { summary: '获取商品总金额' })
  async getPackageTotalMoney(@Body('ids') ids: number[]) {
    if (!ids || ids.length === 0) return 0;
    const money = await this.packageInfoService.getPackageTotalMoney(ids);
    return this.ok({
      totalMoney: money,
      discountMoney: 0,
      currency: 'CNY' // 示例：可添加更多字段
    })
  }
}
